package com.qf.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.qf.ssm.dto.R;
import com.qf.ssm.pojo.Areas;
import com.qf.ssm.service.AreasService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/areas")
@Api(tags = "区域管理模块")
public class AreasController  {

    private AreasService areasService;

    @Autowired
    public void setAreasService(AreasService areasService) {
        this.areasService = areasService;
    }

    @PostMapping("/area")
    @ApiOperation("添加区域")
    public R addAreas(@RequestBody Areas areas)throws Exception{
        areasService.addAreas(areas);
        return R.setOK(areas);
    }

    @GetMapping("/areas/parent")
    public R findParentAreaByStatus(Long status){
        List<Areas> areasList = areasService.findParentAreaByStatus(status);
        return R.setOK(areasList);
    }

    @GetMapping("/areas")
    public R findAllAreasByNameLikeAndStatusEquals(int page,int limit,String areaname, Long status){
        PageInfo<Areas>pageInfo=areasService.findAllAreasByNameLikeAndStatusEquals(page,limit,areaname,status);
           return R.setOK(pageInfo);
    }

    @DeleteMapping("/areas")
    public R deleteAreasByIds(Long [] ids){
        areasService.deleteAreasByIds(ids);
        return R.setOK();
    }

    @PutMapping("/area")
    public R updateAreas(@RequestBody Areas areas){
        areasService.updateAreas(areas);
        return R.setOK();
    }

    @ApiOperation(("根据id获取区域数据"))
    @GetMapping("/areas/{id}")
    public R getAreasById(@PathVariable Long id){
        Areas areas = areasService.getAreasById(id);
        return R.setOK(areas);

    }

    @GetMapping("/area/parent/{id}")
    public R getAreaByParentId(@PathVariable Long id){
        List<Areas> areasList = areasService.getAreaByParentId(id);
        return R.setOK(areasList);
    }
}
